/* Define Component Shortcuts */
let Ci = Components.interfaces;
let Cc = Components.classes;
let Cr = Components.results;

/* Define PR Constants */
const PR_RDONLY        = 0x01
const PR_WRONLY        = 0x02 
const PR_RDWR          = 0x04
const PR_CREATE_FILE   = 0x08
const PR_APPEND        = 0x10
const PR_TRUNCATE      = 0x20
const PR_SYNC          = 0x40
const PR_EXCL          = 0x80
const PR_UINT32_MAX    = 0xffffffff;
const PR_USEC_PER_MSEC = 1000;

/* Define NS Constants */
const NS_PROXY_SYNC    = 0x01;
const NS_PROXY_ASYNC   = 0x02;
const NS_PROXY_ALWAYS  = 0x04;

/* Define SD Log Constants */
const SD_LOG_NONE      = 0;
const SD_LOG_INFO      = 1;
const SD_LOG_WARN      = 2;
const SD_LOG_ERROR     = 3;
const SD_LOG_DEBUG     = 4;
const SD_LOG_DEBUG2    = 5;
const SD_LOG_DEBUG3    = 6;

function log(message, level)
{
	if(level > gPrefs['log.level'])
	{
		return;
	}

	var cs = Cc["@mozilla.org/consoleservice;1"]
		.getService(Ci.nsIConsoleService);

	switch(level)
	{
		case SD_LOG_WARN:
			var consoleMessage = Cc["@mozilla.org/scripterror;1"]
				.createInstance(Ci.nsIScriptError);
			consoleMessage.init(message, null, null, null, null, Ci.nsIScriptError.warningFlag, 'xul');
			cs.logMessage(consoleMessage);
			break;
		case SD_LOG_ERROR:
			var consoleMessage = Cc["@mozilla.org/scripterror;1"]
				.createInstance(Ci.nsIScriptError);
			consoleMessage.init(message, null, null, null, null, Ci.nsIScriptError.errorFlag, 'xul');
			cs.logMessage(consoleMessage);
			break;
		default:
			cs.logStringMessage(message);
	}
}

function readRegistryValue(wrk, value)
{
	switch (wrk.getValueType(value))
	{
		case Ci.nsIWindowsRegKey.TYPE_STRING:
			return wrk.readStringValue(value);
		case Ci.nsIWindowsRegKey.TYPE_BINARY:
			return wrk.readBinaryValue(value);
		case Ci.nsIWindowsRegKey.TYPE_INT:
			return wrk.readIntValue(value);
		case Ci.nsIWindowsRegKey.TYPE_INT64:
			return wrk.readInt64Value(value);
	}
	return null;
}

function toHexString(charCode)
{
	return ("0" + charCode.toString(16)).slice(-2);
}

function notifyObservers(aSubject, aTopic, someData)
{
	var os = Cc["@mozilla.org/observer-service;1"]
		.getService(Ci.nsIObserverService);

	var data = someData;
	if(typeof someData != 'string')
	{
		try
		{
			data = someData.toString();
		}
		catch(ex)
		{
		}
	}

	try
	{
		os.notifyObservers(aSubject, aTopic, data);
	}
	catch(ex)
	{
		for(var i in ex)
		{
			dump(i + ":" + ex[i]);
		}
	}
}


function getProxyOnUIThread(aObject, aInterface)
{
	var mainThread = Cc["@mozilla.org/thread-manager;1"]
		.getService()
		.mainThread;

	var proxyMgr = Cc["@mozilla.org/xpcomproxy;1"]
		.getService(Ci.nsIProxyObjectManager);

	return proxyMgr.getProxyForObject(mainThread, aInterface, aObject, 5);
}

getProxyOnUIThread(new listener(),
	Ci.nsICertVertificationListener);





function listener()
{
}

listener.prototype.QueryInterface = {
	...
}


listener.prototype.notify = {
	function(cert, result)
	{
		DisplayVerificationData(cert, result);
	}
}
